Chain commands
$ ls -al | grep "file_name"
Use the output of one command as arguments for the next
# Find ruby files in app/ that contain the string user. # In the files matched, look for the string "transaction" $ grep "users" -lr app/**/*.rb | xargs grep "transaction"
$ cp source_file target_file (or target_directory/) -r # Recursive -i # Interactive: ask what to do with existing files in the destination -v # Verbose -f # Force
Count words, lines, characters, etc... in a file
$ wc -[w | l | c] your_file.csv
Search one file
$ grep "redmine" /etc/hosts // Search filetypes $ grep -rn "flagged" --include=*.{rb,} .
Flags
-r # search recursively -n # display the line number -l # only output filenames of matched files -B 10 # display 10 lines before the line containing the match -A 10 # display 10 lines after the line containing the match -C 10 # display 10 lines before and after the line containing the match -i # case insensitive -h # Omit filenames in the results -v # Invert, i.e. display lines that do not match
find -name "*.js" # Find js files -wholename "controller" # Look through the entire filename, not just extension -regex # Search with regex pattern -type f # Files -maxdepth X # Go down at most X levels (1 == current directory only) -not -path # Exclude path -delete # Delete the files found # Find all .js files in the app directory minus [[app/assets/.]] $ find app/. -name "*.js" -type f -not -path "*/assets/*" # Find files with a specific pattern and exclude hidden files # 'find' matches the entire path, so append '.*/' to the search $ find . -maxdepth 1 -wholename ".*/application*.js" -not -name ".*" # Find files and move htem $ find . -maxdepth 1 -type f -name "*.sql" | xargs -I '{}' mv '{}' dumps/
du : Display sizes of directories
du -s # Display an entry for each file/directory -h # Display the sizes in a "human readable" way directory_name # Specific directory * # Every directory in pwd # 1: Get directories and files by size # 2: Reverse sort (-r reverse / -h readable) # 3: Only display the top 10 results $ du -sh * | sort -rh | head -10
df : Display space usage at the disk level
df -h # Display the sizes in a "human readable" format $ df -h
Delete in situ lines containing a given string
$ sed -i "/words_in_line/d" ./file_path.log
$ sed -i -- 's/https/http/g' file_path.txt
Safely delete files : rm on steroids! It overwrites the file content
shred -f # Force change permissions to allow writing -n # Number of iterations -v # Verbose (show progress -u # Actually remove file after progress -z # On the last round, overwrite the file with zeros $ shred -uvzn 3 /Documents/file_to_nuke.txt
########################################### # == Count the number of lines for files in a given directory and display the sorted result == # # 1: Find ruby files in the current directory # 2: Count the number of lines # 3: Reverse sort them # 4: Omit the "total" line # 5: Only display the top 10 results $ find . -name "*.rb" -type f | xargs wc -l | sort -rn | grep -v ' total$' | head -10 ########################################### ########################################### # 1: Find all files in a directory # 2: Nuke! $ find directory_path/ -depth -type f -exec shred -uvzn 2 {} \; ########################################### ########################################### # == Copy a command output to the clipboard # Linux $ sudo apt-get install xclip $ cat my_file.txt | xclip -selection clipboard # OSX: pbcopy and pbpaste $ cat ~/.ssh/id_rsa.pub | pbcopy ########################################### ########################################### # == grep filetypes $ grep -rn "string" --include=*.{rb,} ./ $ find . -type f -name "*.rb" | xargs grep -n "string" ###########################################
# Encode file to UTF-8 $ vim +"set nobomb | set fenc=utf8 | x" file_to_encode.txt # Substitute tabs with semi-colons. On OSX, \t doesn't work. ctrl-v then hit tab $ LC_CTYPE=C sed 's/ /;/g' infile.txt outfile.csv
# Kill all processes from a given application $ ps aux | grep firefox | awk '{print $2}' | xargs kill -9
# Batch rename files $ brew install rename $ find . -type f -name "*GUIDELINES*" -exec rename 's/GUIDELINES.md/README.md/' \{\} \;
# Port already in use? $ lsof -i :3000 | awk '{print $2}' | grep -v PID | xargs kill -9
# List all available commands (echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u }} {{{ # Find duplicate files $ find app/controllers/ -type f -name "*.rb" -printf '%f\n' | sort | uniq -c | sort -rn # find app/controllers/ -type f -name "*.rb" -printf '%f\n' # Find ruby files and only print their name # uniq -c # Output unique names and prepend the number of occurences # sort -rn # Sort them in reverse order
# Sort folders by size $ du -cskh * | sort -rh